package p.ithorns.sample.office.pdf;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * TestSelenium
 *
 * @author HtL
 * @date 2025/3/10 11:35
 * @since 1.0.0
 */
public class TestSelenium {

    public static void main(String[] args) throws Exception {
        String url = "http://10.20.32.60:7200/spw-app/special-work/ticket-apply/ticket-record/185?tab=2";
        try {
            testSelenium(url);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    static WebDriver driver;

    static {
        // 驱动和程序下载地址
        // https://googlechromelabs.github.io/chrome-for-testing/#stable
        // https://chromedriver.storage.googleapis.com/index.html
        // https://vikyd.github.io/download-chromium-history-version/#/

        // 设置chromeDriver的位置
        System.setProperty("webdriver.chrome.driver", "D:\\chrome-win\\v85\\chromedriver.exe");
        // 设置驱动
        ChromeOptions options = new ChromeOptions();

        //设置 chrome 的无头模式
        // chrome安装路径(linux用 /usr/bin/google-chrome)
        options.setBinary("D:\\chrome-win\\v85\\chrome.exe");

        options.setPageLoadStrategy(PageLoadStrategy.EAGER);

        // 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
        options.addArguments("--headless");

        // 启动无沙盒模式运行，以最高权限运行
        options.addArguments("--no-sandbox");

        // 优化参数
        // 不加载图片, 提升速度
        //options.addArguments("blink-settings=imagesEnabled=false");
        options.addArguments("--disable-dev-shm-usage");
        // 禁用JavaScript
        // options.addArguments("--disable-javascript");

        // 禁用阻止弹出窗口
        //options.addArguments("--disable-popup-blocking");

        // 禁用gpu渲染
        options.addArguments("--disable-gpu");
        // 禁用扩展
        options.addArguments("disable-extensions");

        // 默认浏览器检查
        options.addArguments("no-default-browser-check");
        // 避免403错误
        options.addArguments("--remote-allow-origins=*");
        // options.addArguments("start-maximized");
        options.addArguments("disable-infobars");
        options.addArguments("--test-type");

        // 禁用保存密码提示框
//        Map<String, Object> prefs = new HashMap<>();
//        prefs.put("credentials_enable_service", false);
//        prefs.put("profile.password_manager_enabled", false);
//        options.setExperimentalOption("prefs", prefs);

        // if(!argument.equals("")) chromeOptions.addArguments(argument);

        // 创建webDriver实例
        driver = new ChromeDriver(options);
//        DevTools tools = driver.getDevTools();
//        tools.createSession();
//        Map<String, Object> map = new HashMap<>();
//        map.put("Authorization",
//                "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiIxNjU4ODY4NTc2NTE5ODEyMTYxIiwicm5TdHIiOiIxVjl4S1UydGxVa2FncFVKekYyWWJIcUMwZVNsclVLRiJ9.6NGVKFn9DtAkVUFo6G4m9EMmJslpFdceWLIRdR1o8GY");
//        map.put("X-app-id", "1760496375387844610");
//        tools.send(Network.setExtraHTTPHeaders(new Headers(map)));
    }

    static void testSelenium(String url) {
        driver.get(url);
        driver.manage().addCookie(new Cookie("domain", "http://10.20.32.60:7200"));
        driver.manage().addCookie(new Cookie("loginType", "WEBSITE"));
//        driver.manage().addCookie(new Cookie("account", "li_si"));
//        driver.manage().addCookie(new Cookie("password", "jee8X+PrxYke+ekOq/tVe4a9UW9KPNdlKtBfcx2vPVSN3BqaCECwot/1oRJno085vGtmb+cmWoESXhUlLunPQA=="));
//        driver.manage().addCookie(new Cookie("rememberMe", "true"));
        driver.manage().addCookie(new Cookie("Authorization",
                "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiI5NDM5NTI1MzM1NTA1MjI3MzI5Iiwicm5TdHIiOiJkNXlSWVM2eGRrWXQ2dE1yaTVVSUpyOU5UbXJDYjVubyJ9.aKy_7p9wqI_2w8ysvOAGEdq4m3cW_qGCPoa9_9Ekj0k"));
        driver.get(url);

        // 隐式等待
        // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        // String pageSource = driver.getPageSource();
        // System.out.println(pageSource);

        // 显示等待元素渲染完成
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
        String html = wait.until(driver -> {
            WebElement element = driver.findElement(By.className("temp-card"));
            return element.getAttribute("outerHTML");
        });

        // 超时异常
//        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".red_box")));
//        WebElement element = driver.findElement(By.cssSelector(".red_box"));

        // 将HTML转换为PDF
        // testIText("http://10.20.32.60:7200", html);

        // 关闭测览器
        driver.quit();
    }

}